package com.animee.jiujiangdict;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.animee.jiujiangdict.bean.WordBean;
import com.animee.jiujiangdict.db.DBManager;
import com.animee.jiujiangdict.utils.URLUtils;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/*
*  文字详情界面
* */
public class WordInfoActivity extends BaseActivity {
    TextView ziTv,pyTv,wubiTv,bihuaTv,bushouTv,jsTv,xxjsTv;
    ListView jsLV;
    ImageView collectIv;   //收藏的图片
    List<String>mDatas;
    private ArrayAdapter<String> adapter;
    private List<String> jijie;   //基本释义的集合数据源
    private List<String> xiangjie;  // 详细解释的集合数据源
    String zi;
    boolean isCollect = false;    //设置标志位，表示这个汉字是否被收藏了
    boolean isExist = false;    // 判断这个汉字是否已经在收藏表当中存在
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_info);
        initView();
        mDatas = new ArrayList<>();
        adapter = new ArrayAdapter<>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mDatas);
        jsLV.setAdapter(adapter);
        Intent intent = getIntent(); //获取上一个界面传递来对象
        zi = intent.getStringExtra("zi");
        String url = URLUtils.getWordurl(zi);
        loadData(url);
        isExist = DBManager.isExistZiInCollwordtb(zi);  // 调用判断的方法
        isCollect = isExist;   //记录初始状态
        setCollIvStyle();
    }
    /* 根据收藏的状态，改变星星的颜色*/
    private void setCollIvStyle() {
        if (isCollect) {
            collectIv.setImageResource(R.mipmap.ic_collection_fs);
        }else{
            collectIv.setImageResource(R.mipmap.ic_collection);
        }
    }

    @Override
    public void onSuccess(String json) {
        WordBean wordBean = new Gson().fromJson(json, WordBean.class);
        WordBean.ResultBean resultBean = wordBean.getResult();
        // 插入数据库
        DBManager.insertWordToWordtb(resultBean);
        notifyView(resultBean); // 显示在View控件上
    }

    public void notifyView(WordBean.ResultBean resultBean) {
        ziTv.setText(resultBean.getZi());
        pyTv.setText(resultBean.getPinyin());
        wubiTv.setText("五笔 : "+resultBean.getWubi());
        bihuaTv.setText("笔画 : "+resultBean.getBihua());
        bushouTv.setText("部首 : "+resultBean.getBushou());
        jijie = resultBean.getJijie();
        xiangjie = resultBean.getXiangjie();
        mDatas.clear();  //清空之前的数据源
        mDatas.addAll(jijie);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        // 联网失败，获取数据库当中对应字的信息对象
        WordBean.ResultBean bean = DBManager.queryWordFromWordtb(zi);
        if (bean!=null) {
            notifyView(bean);
        }
    }

    private void initView() {
        ziTv = findViewById(R.id.word_tv_zi);
        pyTv = findViewById(R.id.word_tv_pinyin);
        wubiTv = findViewById(R.id.word_tv_wubi);
        bihuaTv = findViewById(R.id.word_tv_bihua);
        bushouTv = findViewById(R.id.word_tv_bushou);
        jsTv = findViewById(R.id.word_tv_js);
        xxjsTv = findViewById(R.id.word_tv_xxjs);
        jsLV = findViewById(R.id.word_lv_js);
        collectIv = findViewById(R.id.wordinfo_iv_collection);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wordinfo_iv_back:
                finish();
                break;
            case R.id.wordinfo_iv_collection:    // 收藏的按钮
                isCollect = !isCollect;   //将收藏状态取反
                setCollIvStyle();  // 设置按钮的样式
                break;
            case R.id.word_tv_js:
                jsTv.setTextColor(Color.RED);
                xxjsTv.setTextColor(Color.BLACK);
                mDatas.clear();  //清空之前的数据源
                mDatas.addAll(jijie);
                adapter.notifyDataSetChanged();
                break;
            case R.id.word_tv_xxjs:
                xxjsTv.setTextColor(Color.RED);
                jsTv.setTextColor(Color.BLACK);
                mDatas.clear();  //清空之前的数据源
                mDatas.addAll(xiangjie);
                adapter.notifyDataSetChanged();
                break;
        }
    }

//    在页面销毁时，可以将收藏的汉字插入或者删除
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isExist&&!isCollect) {   //原来的收藏表当中存在数据，但是现在不想收藏
            DBManager.deleteZiToCollwordtb(zi);
        }

        if (!isExist&&isCollect){   //原来收藏表当中不存在，但是想收藏
            DBManager.insertZiToCollwordtb(zi);
        }

    }
}
